package com.qitancai.huazistudentmanagement.service.impl;

import cn.hutool.core.util.StrUtil;
import com.qitancai.huazistudentmanagement.dao.StudentRepository;
import com.qitancai.huazistudentmanagement.entity.Student;
import com.qitancai.huazistudentmanagement.service.StudentSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @ClassName StudentServiceImpl
 * @Description TODO
 * @Author 
 * @Date 2021年06月22日 22:12
 **/
@Service
public class StudentServiceImpl implements StudentSevice {

    @Autowired
    StudentRepository studentRepository;

    @Override
    public Student getStudentById(String id) {
        Optional<Student> optionalStudent = studentRepository.findById(id);
        if (optionalStudent.isPresent()) {
            Student result = optionalStudent.get();
            return result;
        }
        throw new RuntimeException(String.format("查询的学号为: %s 的学生不存在", id));
    }

    @Override
    public Student createStudent(Student student) {
        Optional<Student> optionalStudent = studentRepository.findById(student.getId());
        if (optionalStudent.isPresent()) {
            throw new RuntimeException(String.format("学号 %s 已存在学生使用", student.getId()));
        }
        // 必录校验
        if (StrUtil.isNotBlank(student.getId()) &&
                StrUtil.isNotBlank(student.getName()) &&
                StrUtil.isNotBlank(student.getSex()) &&
                StrUtil.isNotBlank(student.getClassName())) {
            Student result = studentRepository.save(student);
            return result;
        }
        throw new RuntimeException("缺少必录信息！");
    }

    @Override
    public Student updateStudent(Student student) {
        Optional<Student> optionalStudent = studentRepository.findById(student.getId());
        if (optionalStudent.isPresent()) {
            Student savedStudent = optionalStudent.get();
            if (StrUtil.isNotBlank(student.getName())) {
                savedStudent.setName(student.getName());
            }
            if (StrUtil.isNotBlank(student.getSex())) {
                savedStudent.setSex(student.getSex());
            }
            if (StrUtil.isNotBlank(student.getClassName())) {
                savedStudent.setClassName(student.getClassName());
            }
            Student result = studentRepository.save(savedStudent);
            return result;
        }
        throw new RuntimeException(String.format("需要更新的学号为: %s 的学生不存在", student.getId()));
    }

    @Override
    public void deleteStudent(String id) {
        Optional<Student> optionalStudent = studentRepository.findById(id);
        if (optionalStudent.isPresent()) {
            studentRepository.deleteById(id);
            return;
        }
        throw new RuntimeException(String.format("需要删除的学号为: %s 的学生不存在", id));
    }
    @Override
    public Page queryStudent(Pageable pageable) {
        Page<Student> result = studentRepository.findAll(pageable);
        return result;
    }

    @Override
    public Page queryStudentByName(String name, Pageable pageable) {
        Page<Student> result = studentRepository.findByNameContaining(name, pageable);
        return  result;
    }
}
